package com.neusoft.controller;

import com.neusoft.po.CommonResult;
import com.neusoft.po.User;
import com.neusoft.po.UserImg;
import com.neusoft.po.UserPassword;
import com.neusoft.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import static com.neusoft.util.Lock_info.user_lock;
import static com.neusoft.util.MD5.encode;

@RestController
@RequestMapping("/UserController")
@RefreshScope
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/getUserByIdByPass")
    public CommonResult<User> getUserByIdByPass(User user) throws Exception{
        user.setPassword(encode(user.getPassword()));
        return new CommonResult(200,"success",user_lock(userService.getUserByIdByPass(user)));
    }

    @RequestMapping("/getUserById")
    public CommonResult<User> getUserById(User user) throws Exception{
        return new CommonResult(200,"success",user_lock(userService.getUserById(user.getUserId())));
    }

    @RequestMapping("/saveUser")
    public CommonResult<Integer> saveUser(User user) throws Exception{
        user.setPassword(encode(user.getPassword()));
        return new CommonResult(200,"success",userService.saveUser(user));
    }

    @RequestMapping("/changeUserPortrait")
    public CommonResult<Integer> changeUserPortrait(UserImg userImg) throws Exception{
        return new CommonResult(200,"success",userService.changeUserPortrait(userImg));
    }

    @RequestMapping("/modifyPassword")
    public CommonResult<Boolean> modifyPassword(UserPassword userPassword) throws Exception {
        userPassword.setNewPassword(encode(userPassword.getNewPassword()));
        userPassword.setOldPassword(encode(userPassword.getOldPassword()));
        int i = userService.modifyPassword(userPassword);
        if(i == 1){
            return new CommonResult(200,"success",true);
        }
        return new CommonResult(200,"success",false);
    }
}
